//
//  CHAuditDetailController.m
//  CarFile
//
//  Created by 陈浩 on 2017/7/10.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHAuditDetailController.h"
#import "CHAuditModel.h"
#import "CHSectionView.h"
#import "CHFooterView.h"
#import "CHReviewedCell.h"
@interface CHAuditDetailController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) UIImageView *photoImgView;
@property (nonatomic ,strong) NSMutableArray *baseDatas;
@property (nonatomic ,strong) NSMutableArray *contactDatas;
@property (nonatomic ,strong) NSMutableArray *groups;
@property (nonatomic ,strong) NSMutableDictionary *dict;
@property (nonatomic ,strong) NSMutableDictionary *operatestate;   //  经营状态
@property (nonatomic ,strong) UIView *bottomView;
@property (nonatomic ,strong) UIImageView *imgView;
@property (nonatomic ,strong) UIButton *coverBtn;
@property (nonatomic ,copy) NSString *photoUrlStr;
@end

@implementation CHAuditDetailController
-(NSMutableDictionary *)operatestate{
    if (!_operatestate) {
        _operatestate = [NSMutableDictionary dictionary];
        [_operatestate setObject:@"营业" forKey:@"1"];
        [_operatestate setObject:@"停业" forKey:@"2"];
        [_operatestate setObject:@"整改" forKey:@"3"];
        [_operatestate setObject:@"停业整顿" forKey:@"4"];
        [_operatestate setObject:@"歇业" forKey:@"5"];
        [_operatestate setObject:@"注销" forKey:@"6"];
        [_operatestate setObject:@"其他" forKey:@"9"];
    }
    
    return _operatestate;
    
}




-(NSMutableDictionary *)dict{
    if (!_dict) {
        _dict = [NSMutableDictionary dictionary];
        [_dict setObject:@"内资" forKey:@"100"];
        [_dict setObject:@"国有全资" forKey:@"110"];
        [_dict setObject:@"集体全资" forKey:@"120"];
        [_dict setObject:@"股份合作" forKey:@"130"];
        [_dict setObject:@"联营" forKey:@"140"];
        [_dict setObject:@"国有联营" forKey:@"141"];
        [_dict setObject:@"集体联营" forKey:@"142"];
        [_dict setObject:@"国有与集体联营" forKey:@"143"];
        [_dict setObject:@"其他联营" forKey:@"149"];
        [_dict setObject:@"有限责任（公司）" forKey:@"150"];
        [_dict setObject:@"其他有限责任（公司）" forKey:@"159"];
        [_dict setObject:@"股份有限（公司）" forKey:@"160"];
        [_dict setObject:@"私有" forKey:@"170"];
        [_dict setObject:@"私有独资" forKey:@"171"];
        [_dict setObject:@"私有合伙" forKey:@"172"];
        [_dict setObject:@"私营有限责任（公司）" forKey:@"173"];
        [_dict setObject:@"私营股份有限（公司）" forKey:@"174"];
        [_dict setObject:@"个体经营" forKey:@"175"];
        [_dict setObject:@"其他私有" forKey:@"179"];
        [_dict setObject:@"其他内资" forKey:@"190"];
        [_dict setObject:@"港、澳、台投资" forKey:@"200"];
        [_dict setObject:@"内地和港、澳或台合资" forKey:@"210"];
        [_dict setObject:@"内地和港、澳或台合作" forKey:@"220"];
        [_dict setObject:@"港、澳或台独资" forKey:@"230"];
        [_dict setObject:@"港、澳或台投资股份有限（公司）" forKey:@"240"];
        [_dict setObject:@"其他港澳台投资" forKey:@"290"];
        
    }
    return _dict;
    
}
-(NSMutableArray *)groups{
    if (!_groups) {
        _groups = [NSMutableArray array];
    }
    return _groups;
}

-(NSMutableArray *)contactDatas{
    if (!_contactDatas) {
        _contactDatas = [NSMutableArray array];
    }
    return _contactDatas;
    
}

-(NSMutableArray *)baseDatas{
    if (!_baseDatas) {
        _baseDatas = [NSMutableArray array];
    }
    return _baseDatas;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"企业详情";
    self.view.backgroundColor = [UIColor whiteColor];
    
    if ([self.model.state  isEqualToString:@"2"]) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, UIScreenW,UIScreenH-64) style:UITableViewStyleGrouped];
        CHFooterView *view = [[CHFooterView alloc] initWithFrame:CGRectMake(0, 0, UIScreenW, 150)];
        view.textView.text = self.model.nopassreason;
        self.tableView.tableFooterView = view;
    }else{
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, UIScreenW,UIScreenH-26) style:UITableViewStyleGrouped];
    }
    self.photoImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, UIScreenW, 130)];
    self.photoImgView.image = [UIImage imageNamed:@"placeholder"];
    self.photoImgView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    [self.photoImgView addGestureRecognizer:tap];
    self.tableView.tableHeaderView = self.photoImgView;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
   
    [self.view addSubview:self.tableView];
    
    [BQActivityView showActiviTy];
    
    [self getData];

    
}



-(void)tapGesture:(UITapGestureRecognizer *)tap{
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    // 创建遮罩层
    UIButton *coverBtn = [[UIButton alloc] init];
    
    //  设置遮罩层的背景颜色
    coverBtn.backgroundColor = [UIColor blackColor];
    //  设置frame
    coverBtn.frame = window.bounds;
    //  添加到控制器view中
    [window addSubview:coverBtn];
    //  给遮罩层属性赋值
    self.coverBtn = coverBtn;
    //  设置遮罩层透明度
    coverBtn.alpha = 0.0;
    
    //  让图片按钮显示在控制器view最前面 Front前面
    UIImageView *imgView = [[UIImageView alloc] init];
    [imgView sd_setImageWithURL:[NSURL URLWithString:self.photoUrlStr] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    CGRect imgViewFrm = [self.photoImgView.superview convertRect:self.photoImgView.frame toView:window];
    
    imgView.frame = imgViewFrm;
    
    
    [window addSubview:imgView];
    self.imgView = imgView;
    //    [window bringSubviewToFront:self.headImgView];
    
    [UIView animateWithDuration:0.5 animations:^{
        self.imgView.frame = CGRectMake(0, (UIScreenH - UIScreenW)/2, UIScreenW, UIScreenW);
        //        设置遮罩层透明度
        coverBtn.alpha = 0.5;
    }];
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(smallImage)];
    [imgView addGestureRecognizer:tap1];
    
    //  监听遮罩层按钮的点击
    [coverBtn addTarget:self action:@selector(smallImage) forControlEvents:UIControlEventTouchUpInside];
    
}


-(void)smallImage{
    CGRect imgViewFrm = [self.photoImgView.superview convertRect:self.photoImgView.frame toView:nil];
    [UIView animateWithDuration:0.5 animations:^{
        //  还原图片按钮
        self.imgView.frame = imgViewFrm;
        //      设置遮罩层慢慢变透明
        self.coverBtn.alpha = 0.0;
        
    } completion:^(BOOL finished) {
        //      当动画执行完毕就会调用这个block
        //  移除遮罩层
        [self.coverBtn removeFromSuperview];
        [self.imgView removeFromSuperview];
    }];
    
}









-(void)getData{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"areaid"] = [Util getValue:@"areaid"];
    params[@"usertype"] = [Util getValue:@"usertype"];
    params[@"companyid"] = self.model.companyid;
    params[@"user"] = [Globle getInstance].user;
    params[@"sign"] = [DESCript encrypt:self.model.companyid encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    [[Globle getInstance].service requestWithServiceName:@"lcipappcheckcompanydetails" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
        
        [BQActivityView hideActiviTy];
        NSLog(@"审核内容%@",result);
        if ([result[@"code"] isEqualToString:@"1"]) {
            [self.photoImgView sd_setImageWithURL:[NSURL URLWithString:result[@"data"][@"companyphoto"]] placeholderImage:[UIImage imageNamed:@"placeholder"]];
            self.photoUrlStr = result[@"data"][@"companyphoto"];
            
            NSString *text = nil;
            [self setTitle:@"维修企业名称" andResultTitle:result[@"data"][@"name"]];
            [self setTitle:@"统一社会信用代码" andResultTitle:result[@"data"][@"orgnumber"]];
            [self setTitle:@"经营状态" andResultTitle:[self.operatestate objectForKey:result[@"data"][@"operatestate"]]];
            [self setTitle:@"企业经济类型" andResultTitle: [self.dict objectForKey:result[@"data"][@"businesstype"]]];
            [self setTitle:@"道路运输经营许可证" andResultTitle:result[@"data"][@"industryid"]];
            [self setTitle:@"道路运输经营许可证发证日期" andResultTitle:[self getDateWithDateStr: result[@"data"][@"certificatefirsttime"]]];
            [self setTitle:@"道路运输经营许可证开始日期" andResultTitle: [self getDateWithDateStr: result[@"data"][@"certificatestarttime"]]];
            [self setTitle:@"道路运输经营许可证结束日期" andResultTitle:[self getDateWithDateStr: result[@"data"][@"certificateendtime"]]];
            text = nil;
            if ([result[@"data"][@"levels"]  isEqualToString:@"01"]) {
                text = @"一类维修经营业务";
            }else if([result[@"data"][@"levels"]  isEqualToString:@"02"]){
                text = @"二类维修经营业务";
            }else if ([result[@"data"][@"levels"]  isEqualToString:@"03"]){
                text = @"三类维修经营业务";
            }else{
                text = @"其他";
            }
            [self setTitle:@"维修企业经营业务类别" andResultTitle:text];
            [self setTitle:@"维修企业编码" andResultTitle:result[@"data"][@"companycode"]];
            
   
            text = nil;
            if ([result[@"data"][@"isshop"]  isEqualToString:@"0"]) {
                text = @"否";
            
            }else{
                text = @"是";
             
            }
            [self setTitle:@"是否为4s店或特约维修站" andResultTitle:text];
            [self setTitle:@"主修品牌" andResultTitle:result[@"data"][@"magorbrands"]];
            [self setTitle:@"企业经营范围" andResultTitle:result[@"data"][@"businessrange"]];
            [self setTitle:@"企业地址" andResultTitle:result[@"data"][@"address"]];
            [self setTitle:@"邮政编码" andResultTitle:result[@"data"][@"linkzip"]];
            
            [self.groups addObject:self.baseDatas];
            [self setTitle1:@"企业联系人姓名" andResultTitle:result[@"data"][@"linkman"]];
            [self setTitle1:@"企业联系人联系方式" andResultTitle:result[@"data"][@"linktel"]];
            [self setTitle1:@"企业负责人姓名" andResultTitle:result[@"data"][@"legalname"]];
            [self setTitle1:@"企业负责人联系方式" andResultTitle:result[@"data"][@"legaltel"]];
            [self setTitle1:@"业务电话" andResultTitle:result[@"data"][@"busphone"]];
            [self setTitle1:@"企业邮箱" andResultTitle:result[@"data"][@"emails"]];
            [self.groups addObject:self.contactDatas];
            [self.tableView reloadData];
        }
    }];
    
}


-(void)setTitle1:(NSString *)title andResultTitle:(NSString *)result{
    
    CHAuditModel *model = [[CHAuditModel alloc] init];
    model.title = title;
    model.result = result;
    [self.contactDatas addObject:model];
}



-(void)setTitle:(NSString *)title andResultTitle:(NSString *)result{
    
    CHAuditModel *model = [[CHAuditModel alloc] init];
    model.title = title;
    model.result = result;
    [self.baseDatas addObject:model];
}

-(NSString *)getDateWithDateStr:(NSString *)str{
    NSArray *array = [str componentsSeparatedByString:@" "]; //从字符A中分隔成2个元素的数组
    return array[0];
}


-(UILabel *)setLabelRight:(NSString *)title Left:(NSString *)title1{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, UIScreenW - 30, 44)];
    label.font = [UIFont systemFontOfSize:14];
    label.textColor = [UIColor colorWithHexString:@"999999" alpha:1.0f];
    label.text = [NSString stringWithFormat:@"  %@  %@",title,title1.length > 0 ? title1:@"--"];
    return label;
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.groups.count;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [self.groups[section] count];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    CHReviewedCell *cell = [CHReviewedCell cellForTableView:tableView];
    if (indexPath.section ==0 ) {
        cell.model = self.baseDatas[indexPath.row];
    }else{
        cell.model = self.contactDatas[indexPath.row];
    }


    
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 60;
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    CHSectionView *sectionView = [CHSectionView sectionView];
    if (section == 0) {
        sectionView.sectionLabel.text = @"企业基础信息";
    }else if(section == 1){
        sectionView.sectionLabel.text = @"企业联系方式";
    }
    return sectionView;
    
}



#pragma mark LongriseRouterDelegater代理回调方法
-(void)setParams:(id)obj block:(LongriseRouterBlock)block
{
    
    if (obj && [obj isKindOfClass:[NSMutableDictionary class]]) {
        
        NSMutableDictionary *dict = obj;
        self.model = dict[@"model"];
    }
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
